package com.javaautotest1;

import jdk.internal.util.xml.impl.SAXParserImpl;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.text.SimpleDateFormat;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2023-01-13
 * Time: 9:47
 */
public class firstautotest {
    //使用驱动驱使浏览器打开
    ChromeDriver driver = new ChromeDriver();
        //开始,打开百度
    public void start() throws InterruptedException {
        Thread.sleep(3000);
        //使用get方法访问按百度
        driver.get("https://www.baidu.com");
        Thread.sleep(3000);

    }
    //释放驱动对象
    public void end() throws InterruptedException {
        Thread.sleep(3000);
        //quit释放掉驱动对象,关闭llq
        driver.quit();

    }
    //定位:元素必须唯一才能定位成功
    public void location() throws InterruptedException {
//        //css选择器定位
//        String newsText = driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).getText();
//        System.out.println("newsText:"+newsText);

        //xpath定位
//        driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("selenium");
//        driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
//        Thread.sleep(3000);
//        driver.findElement(By.xpath("//*[@id=\"1\"]/div/div/h3/a"));
       String newsText =  driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[2]/a/span[2]")).getText();
        System.out.println(newsText);
    }
/**
 * 元素操作
 */
//public void control() throws InterruptedException {
////    //click点击
////    Thread.sleep(3000);
////    driver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
////    Thread.sleep(3000);
////    driver.findElement(By.cssSelector("#su")).click();
//
//    //submit提交 注意submit和click 如果都是操作按钮,那两者都一样,如果是操作urlsubmit就无法成功,click能成功
//    // Thread.sleep(3000);
////    driver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
////    Thread.sleep(3000);
////    driver.findElement(By.cssSelector("#su")).submit();
//
////    //如果是操作urlsubmit就无法成功,click能成功
////    driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title")).submit();
//
//
////    //send_keys模拟按键进输入
////    driver.findElement(By.cssSelector("#kw")).sendKeys("\\\\____^^^!!!$#%#%^");
////    Thread.sleep(3000);
//
////    //clear清空
////    driver.findElement(By.cssSelector("#kw")).sendKeys("java最好的编程语言");
////    Thread.sleep(3000);
////    driver.findElement(By.cssSelector("#kw")).click();
////    Thread.sleep(3000);
////    driver.findElement(By.cssSelector("#kw")).sendKeys("java是一门优秀的变成语言");
//
//    //getText获取文本内容
////    String text = driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title")).getText();
////    System.out.println(text);
////    //百度一下按钮里的"百度一下"是按钮的value属性值,不是文本内容,想要获取属性值得使用getAttribute
////    String bdText1 = driver.findElement(By.cssSelector("#su")).getAttribute("type");
////    String bdText2 = driver.findElement(By.cssSelector("#su")).getAttribute("id");
////    String bdText3 = driver.findElement(By.cssSelector("#su")).getAttribute("value");
////    String bdText4 = driver.findElement(By.cssSelector("#su")).getAttribute("class");
////    System.out.println(bdText1);
////    System.out.println(bdText2);
////    System.out.println(bdText3);
////    System.out.println(bdText4);
//}

    //代码的执行速度很快,而页面渲染需要时间.
    //代码执行完了,界面还没与渲染出来就会报找不到的异常
    public void wait_test() throws InterruptedException {
        //强制等待
//        driver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
//        driver.findElement(By.cssSelector("#su")).click();
//        Thread.sle    ep(3000);//强制暂停,固定的等待时间必须根据页面渲染的速度来决定,太短了页面也可能没有渲染出来,还是会报错
//        driver.findElement(By.cssSelector("#\\31  > div > div > div > div > div.cos-row.row-text_Johh7.row_5y9Az > div > a > div > p > span > span"));
//        //隐式等待
//        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
//        driver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
//        driver.findElement(By.cssSelector("#su")).click();
//        driver.findElement(By.cssSelector("#\\31  > div > div > div > div > div.cos-row.row-text_Johh7.row_5y9Az > div > a > div > p > span > span"));

        //显示等待
        driver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
        driver.findElement(By.cssSelector("#su")).click();
        WebDriverWait foo = new WebDriverWait(driver,Duration.ofSeconds(10));
        //foo.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\31  > div > div > div > div > div.cos-row.row-text_Johh7.row_5y9Az > div > a > div > p > span > span")));
        foo.until(ExpectedConditions.textToBe(By.cssSelector("#\\31  > div > div > div > div > div.cos-row.row-text_Johh7.row_5y9Az > div > a > div > p > span > span"),"迪丽热巴1"));
    }

    /**
     * 隐式等待和显示等待一起使用
     * 不建议隐式等待和显示等待一起使用,两种模式实际等待的时间并不是5+10or10而是有很多多种可能性:10,11....
     */
    public void waitTwoPattern(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
        System.out.println(sdf.format(System.currentTimeMillis()));
        //隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
       // driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title"));
        //显示等待
        WebDriverWait foo = new WebDriverWait(driver,Duration.ofSeconds(10));
        try{
            foo.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content")));
        }catch (Exception e){
            System.out.println("noSuchElement");
        }
        System.out.println(sdf.format(System.currentTimeMillis()));
    }






//    //1.第一个简单的自动化示例
//    public void curry() throws InterruptedException {
//        //使用驱动驱使浏览器打开
//        ChromeDriver driver = new ChromeDriver();
//        Thread.sleep(3000);
//        //使用get方法访问按百度
//        driver.get("https://www.baidu.com");
//        Thread.sleep(3000);
//        //使用findElement和By.cssSelector来找到输入框,并且输入关键词
//        driver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
//        Thread.sleep(3000);
//        //使用findElement找到百度一下按钮,click进行点击
//        driver.findElement(By.cssSelector("#su")).click();
//        Thread.sleep(3000);
//        //quit释放掉驱动对象,关闭llq
//        driver.quit();
//
//    }

}
